package contest;

import java.io.*;
import java.util.*;

public class WatchinTVA {
	public static void main(String args[]){
		Scanner sc = new Scanner(new InputStreamReader(System.in));
		while(true){
			int value = sc.nextInt();
			if(value==0)break;
			String[] name = new String[value];
			int[] week = new int[value];
			int[] time = new int[value];
			for(int i=0;i<value;i++){
				name[i] = sc.next();
				week[i] = sc.nextInt();
				time[i] = sc.nextInt();
			}
			int need = sc.nextInt();
			String[] needname = new String[need];
			for(int i=0;i<value;i++){
				needname[i] = sc.next();
			}
			
		}
	}
}
